********************************************************************************
*                                                                              *
*              FIRM-LEVEL ANALYSES OF THE IMPACT OF ROBOT ADOPTION             *
*                                                                              *
********************************************************************************

cd "H:\"

// Variable list for fixed effects regressions
global feweightvar hoursworked09
global fecvar beid sbi4#year mun#year c.logvalueaddedpworker09#year c.logworkers09#year
global fecvarcompete beid sbi2#year mun#year c.logvalueaddedpworker09#year c.logworkers09#year
global cluster beid#year

set seed 89435623 

// What do we do below:
*1) Basic panel regressions
*2) Control for computers and machines
*3) Other robot threshold values and include robot investments continuously
*4) Robustness with respect to International Trade Register data 
*5) Event studies
*6) Leads and lags
*7) Exclude window around robot implementation
*8) Placebo treatment
*9) Oster's bias-adjusted estimates
*10) SUTVA issues
*11) Negative weights in DID
*12) Unweighted regressions
*13) Keep all firms
*14) Estimate for long yearly panel 2004-2019
*15) Within-firm wage inequality


////////////////////////////////////////////////////////////////////////////////
/////////////////////////1) PANEL DATA ANALYSES//////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

* Create Table B1, Panel B

use "Robots and Workers\Data\BEID_PS.dta", clear
keep if manufacturing == 1

reghdfe logvalueadded robots [weight=$feweightvar], absorb($fecvar) cluster($cluster)
	estimates store weighted1
	scalar β1 = _b[robots]
reghdfe laborshare robots [weight=$feweightvar], absorb($fecvar)  cluster($cluster)
	estimates store weighted3
	scalar β3 = _b[robots]
reghdfe loghwage robots [weight=$feweightvar], absorb($fecvar)  cluster($cluster)
	estimates store weighted4
	scalar β4 = _b[robots]
reghdfe loghoursworked robots [weight=$feweightvar], absorb($fecvar)  cluster($cluster)
	estimates store weighted5
	scalar β5 = _b[robots]

qui estimates restore weighted1
qui outreg2 using "Robots and Workers\Results\Panelregressions", label aster replace word tex ctitle(GVA \textit{(log)}) nocon addnote(Notes: We include firm and 4-digit industry-by-year and municipality-by-year fixed effects in all specifications. We also control for linear trends of the log value added per worker in 2009, as well as linear trends of the log of workers in 2009.)
qui estimates restore weighted3
qui outreg2 using "Robots and Workers\Results\Panelregressions", label aster append word tex ctitle(Labor share) nocon
qui estimates restore weighted4
qui outreg2 using "Robots and Workers\Results\Panelregressions", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore weighted5
    outreg2 using "Robots and Workers\Results\Panelregressions", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon	


////////////////////////////////////////////////////////////////////////////////
//////////////////////2) ROBOT ADOPTION - COMPUTERS AND MACHINES////////////////
////////////////////////////////////////////////////////////////////////////////	

* Create Table B2

global threshold 95

use "Robots and Workers\Data\BEID_PS.dta", clear
keep if manufacturing == 1
merge m:1 beid year using "Robots and Workers\Data\BEID_shiftshare.dta", nogen keep(1 3)
merge 1:1 beid year using "Robots and Workers\Data\BEID_Sharemostaffected.dta", nogen keep(1 3)

g computers = .
g machines = .
local y = 1
qui forvalues year = 2009(1)2020{
	nois _dots `y' 0
	centile inv_computers, centile($threshold), if year == `year'
	replace computers = inv_computers > r(c_1) if year == `year'
	centile inv_mach, centile($threshold), if year == `year'
	replace machines = inv_mach > r(c_1) if year == `year'
}

sort beid year 
foreach var in computers machines {
	capture drop `var'_window
	g `var'_window = `var'[_n+1]==1 & beid==beid[_n+1] & year[_n+1]-year<=2
	replace `var'_window = `var'[_n+2]==1 & beid==beid[_n+2] & year[_n+2]-year<=2 if `var'_window==0
	replace `var'_window = `var'  if `var'_window==0
	replace `var'_window = `var'[_n-1]==1 & beid==beid[_n-1] & year-year[_n-1]<=2 if `var'_window==0
	replace `var'_window = `var'[_n-2]==1 & beid==beid[_n-2] & year-year[_n-2]<=2 if `var'_window==0

	replace computers = 1 if computers[_n-1]==1 & beid==beid[_n-1]
	replace machines = 1 if machines[_n-1]==1 & beid==beid[_n-1]
}

egen beidXyear = group(beid year)
g sbi2 = substr(sbi,1,2)
destring sbi2, force replace
egen sectorXyear = group(sector year)

foreach var in computers machines {
	g robotsX`var' = robots*`var'
	g competeX`var' = compete*`var'
	g robotsexposureX`var' = robotsexposure_tmin5*`var'
}

foreach var in routine bluecollar meffect replaceable replaceable_orig {
	capture drop robotsXsh`var'
	g robotsXsh`var' = robots*sh`var'
	g computersXsh`var' = computers*sh`var'
	g machinesXsh`var' = machines*sh`var'
}

reghdfe logvalueadded robots computers machines [weight=$feweightvar], absorb($fecvar) cluster($cluster)
estimates store weighted1
reghdfe laborshare robots computers machines [weight=$feweightvar], absorb($fecvar) cluster($cluster)
estimates store weighted2
reghdfe loghwage robots computers machines [weight=$feweightvar], absorb($fecvar) cluster($cluster)
estimates store weighted3
reghdfe loghoursworked robots computers machines [weight=$feweightvar], absorb($fecvar) cluster($cluster)
estimates store weighted4

qui estimates restore weighted1
qui outreg2 using "Robots and Workers\Results\Panelregressions_ControlforCompMach", label aster replace word tex ctitle(GVA \textit{(log)}) nocon addnote(Notes: We include firm and 4-digit industry-by-year and municipality-by-year fixed effects in all specifications. We also control for linear trends of the log value added per worker in 2009, as well as linear trends of the log of workers in 2009.)
qui estimates restore weighted2
qui outreg2 using "Robots and Workers\Results\Panelregressions_ControlforCompMach", label aster append word tex ctitle(Labor share) nocon
qui estimates restore weighted3
qui outreg2 using "Robots and Workers\Results\Panelregressions_ControlforCompMach", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore weighted4
    outreg2 using "Robots and Workers\Results\Panelregressions_ControlforCompMach", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
	
	
////////////////////////////////////////////////////////////////////////////////
///////////////////////3) MEASUREMENT OF ROBOT ADOPTION/////////////////////////
////////////////////////////////////////////////////////////////////////////////


use "Robots and Workers\Data\BEID_PS.dta", clear
keep if manufacturing == 1

* Create Table B3, columns (1-4)

capture drop robots_c*
g robots_c50000 = cimp_robots >=50000
	
reghdfe logvalueadded robots_c* [weight=$feweightvar], absorb($fecvar) cluster($cluster)
	estimates store weighted1
reghdfe laborshare robots_c* [weight=$feweightvar], absorb($fecvar)  cluster($cluster)
	estimates store weighted3
reghdfe loghwage robots_c* [weight=$feweightvar], absorb($fecvar)  cluster($cluster)
	estimates store weighted4
reghdfe loghoursworked robots_c* [weight=$feweightvar], absorb($fecvar)  cluster($cluster)
	estimates store weighted5


qui estimates restore weighted1
qui outreg2 using "Robots and Workers\Results\Panelregressions_robotsthresholdhigher", label aster replace word tex ctitle(GVA \textit{(log)}) nocon addnote(Notes: We include firm and 4-digit industry-by-year and municipality-by-year fixed effects in all specifications. We also control for linear trends of the log value added per worker in 2009, as well as linear trends of the log of workers in 2009.)
qui estimates restore weighted3
qui outreg2 using "Robots and Workers\Results\Panelregressions_robotsthresholdhigher", label aster append word tex ctitle(Labor share) nocon
qui estimates restore weighted4
qui outreg2 using "Robots and Workers\Results\Panelregressions_robotsthresholdhigher", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore weighted5
    outreg2 using "Robots and Workers\Results\Panelregressions_robotsthresholdhigher", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon	
	
* Create Table B3, columns (5-8)	

g ihs_cimp_robots = ln(cimp_robots+sqrt(cimp_robots^2+1))

reghdfe logvalueadded ihs_cimp_robots [weight=$feweightvar], absorb($fecvar) cluster($cluster)
	estimates store weighted1
reghdfe laborshare ihs_cimp_robots [weight=$feweightvar], absorb($fecvar)  cluster($cluster)
	estimates store weighted3
reghdfe loghwage ihs_cimp_robots [weight=$feweightvar], absorb($fecvar)  cluster($cluster)
	estimates store weighted4
reghdfe loghoursworked ihs_cimp_robots [weight=$feweightvar], absorb($fecvar)  cluster($cluster)
	estimates store weighted5

qui estimates restore weighted1
qui outreg2 using "Robots and Workers\Results\Panelregressions_robotsvalues", label aster replace word tex ctitle(GVA \textit{(log)}) nocon addnote(Notes: We include firm and 4-digit industry-by-year and municipality-by-year fixed effects in all specifications. We also control for linear trends of the log value added per worker in 2009, as well as linear trends of the log of workers in 2009.)
qui estimates restore weighted3
qui outreg2 using "Robots and Workers\Results\Panelregressions_robotsvalues", label aster append word tex ctitle(Labor share) nocon
qui estimates restore weighted4
qui outreg2 using "Robots and Workers\Results\Panelregressions_robotsvalues", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore weighted5
    outreg2 using "Robots and Workers\Results\Panelregressions_robotsvalues", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon	
	
			
	
	
////////////////////////////////////////////////////////////////////////////////
/////////////////////4) ROBUTNESS WITH RESPECT TO IHG DATA//////////////////////
////////////////////////////////////////////////////////////////////////////////

use "Robots and Workers\Data\BEID_PS.dta", clear
keep if manufacturing == 1

* Create columns (1-4) in Table B4
reghdfe logvalueadded robots [weight=$feweightvar], absorb($fecvar) cluster($cluster), if EUthreshold==1
estimates store weighted1
reghdfe laborshare robots [weight=$feweightvar], absorb($fecvar)  cluster($cluster), if EUthreshold==1
estimates store weighted3
reghdfe loghwage robots [weight=$feweightvar], absorb($fecvar)  cluster($cluster), if EUthreshold==1
estimates store weighted4
reghdfe loghoursworked robots [weight=$feweightvar], absorb($fecvar) cluster($cluster), if EUthreshold==1
estimates store weighted5

qui estimates restore weighted1
qui outreg2 using "Robots and Workers\Results\Panelregressions_thresh", label aster replace word tex ctitle(GVA \textit{(log)}) nocon
qui estimates restore weighted3
qui outreg2 using "Robots and Workers\Results\Panelregressions_thresh", label aster append word tex ctitle(Labor share) nocon
qui estimates restore weighted4
qui outreg2 using "Robots and Workers\Results\Panelregressions_thresh", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore weighted5
    outreg2 using "Robots and Workers\Results\Panelregressions_thresh", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon	
	
* Create columns (5-8) in Table B4
preserve
keep if imp_tot>0 & EUthreshold==1
	reghdfe logvalueadded robots [weight=$feweightvar], absorb($fecvar) cluster($cluster)
	estimates store weighted1
	reghdfe laborshare robots [weight=$feweightvar], absorb($fecvar)  cluster($cluster)
	estimates store weighted3
	reghdfe loghwage robots [weight=$feweightvar], absorb($fecvar)  cluster($cluster)
	estimates store weighted4
	reghdfe loghoursworked robots [weight=$feweightvar], absorb($fecvar)  cluster($cluster)
	estimates store weighted5
restore

qui estimates restore weighted1
qui outreg2 using "Robots and Workers\Results\Panelregressions_thresh", label aster append word tex ctitle(GVA \textit{(log)}) nocon addnote(Notes: We include firm and 4-digit industry-by-year and municipality-by-year fixed effects in all specifications. We also control for linear trends of the log value added per worker in 2009, as well as linear trends of the log of workers in 2009.)
qui estimates restore weighted3
qui outreg2 using "Robots and Workers\Results\Panelregressions_thresh", label aster append word tex ctitle(Labor share) nocon
qui estimates restore weighted4
qui outreg2 using "Robots and Workers\Results\Panelregressions_thresh", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore weighted5
    outreg2 using "Robots and Workers\Results\Panelregressions_thresh", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon	

* Create columns (9-12) in Table B4
preserve
	bysort beid: egen robots_exp = max(cexp_robots)
	bysort beid: egen robots_reexp = max(creexp_robots)
	drop if robots_exp > 0 | robots_reexp > 0

	reghdfe logvalueadded robots [weight=$feweightvar], absorb($fecvar) cluster($cluster)
	estimates store weighted1
	reghdfe laborshare robots [weight=$feweightvar], absorb($fecvar)  cluster($cluster)
	estimates store weighted3
	reghdfe loghwage robots [weight=$feweightvar], absorb($fecvar)  cluster($cluster)
	estimates store weighted4
	reghdfe loghoursworked robots [weight=$feweightvar], absorb($fecvar) cluster($cluster)
	estimates store weighted5
restore 

qui estimates restore weighted1
qui outreg2 using "Robots and Workers\Results\Panelregressions_thresh", label aster append word tex ctitle(GVA \textit{(log)}) nocon
qui estimates restore weighted3
qui outreg2 using "Robots and Workers\Results\Panelregressions_thresh", label aster append word tex ctitle(Labor share) nocon
qui estimates restore weighted4
qui outreg2 using "Robots and Workers\Results\Panelregressions_thresh", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore weighted5
    outreg2 using "Robots and Workers\Results\Panelregressions_thresh", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon	


////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////5) EVENT STUDIES///////////////////////////////
////////////////////////////////////////////////////////////////////////////////

* Create Figure B1

use "Robots and Workers\Data\BEID_PS.dta", clear
keep if manufacturing == 1
g robots_gttmin5 = 1-robots_tmin5-robots_tmin4-robots_tmin3-robots_tmin2-robots_tmin1-robots
g robots_gttmin4 = 1-robots_tmin4-robots_tmin3-robots_tmin2-robots_tmin1-robots
g robots_gttmin3 = 1-robots_tmin3-robots_tmin2-robots_tmin1-robots

//// Define sheet name
global sheetname EventStudiesFirms

//// Weighted results with post-event
putexcel set "Robots and Workers\Results\Figures.xlsx", modify sheet($sheetname)
putexcel A1 = "N=45,576"
putexcel A24 = "WEIGHTED RESULTS WITH POST"
putexcel A25 = "Years before/after robot adoption"
putexcel A26 = "All"
putexcel A27 = "All"
putexcel A28 = -5
putexcel A29 = -5
putexcel A30 = -4
putexcel A31 = -4
putexcel A32 = -3
putexcel A33 = -3
putexcel A34 = -2
putexcel A35 = -2
putexcel A36 = -1
putexcel A37 = -1
putexcel A38 = 0
putexcel A39 = 0
putexcel A40 = 1	
putexcel A41 = 1	
putexcel A42 = 2	
putexcel A43 = 2

local i = 1
foreach var in logvalueadded laborshare loghwage loghoursworked {
	reghdfe `var' robots ,absorb($fecvar) cluster($cluster), if robots_tmin5!=. 
	local letter = char(65+`i')
	putexcel `letter'25 = "`var'"
	putexcel `letter'26 = _b[robots]
	putexcel `letter'27 = _b[robots]
	
	reghdfe `var' robots_gttmin4 robots_tmin4 robots_tmin3 robots_tmin1 robots_t0 robots_t1 robots_gte2, absorb($fecvar) cluster($cluster), if robots_tmin5!=. 
	putexcel `letter'28 = _b[robots_gttmin4]
	putexcel `letter'29 = _b[robots_gttmin4]
	putexcel `letter'30 = _b[robots_tmin4]
	putexcel `letter'31 = _b[robots_tmin4]
	putexcel `letter'32 = _b[robots_tmin3]
	putexcel `letter'33 = _b[robots_tmin3]
	putexcel `letter'34 = 0
	putexcel `letter'35 = 0
	putexcel `letter'36 = _b[robots_tmin1]
	putexcel `letter'37 = _b[robots_tmin1]
	putexcel `letter'38 = _b[robots_t0]
	putexcel `letter'39 = _b[robots_t0]
	putexcel `letter'40 = _b[robots_t1]
	putexcel `letter'41 = _b[robots_t1]	
		
	reghdfe `var' robots ,absorb($fecvar) cluster($cluster), if robots_tmin5!=. 
	local letter = char(66+`i')
	putexcel `letter'26 = _b[robots]-1.96*_se[robots]
	putexcel `letter'27 = _b[robots]+1.96*_se[robots]
	
	reghdfe `var' robots_gttmin4 robots_tmin4 robots_tmin3 robots_tmin1 robots_t0 robots_t1 robots_gte2, absorb($fecvar) cluster($cluster), if robots_tmin5!=.
	putexcel `letter'28 = _b[robots_gttmin4]+1.96*_se[robots_gttmin4]
	putexcel `letter'29 = _b[robots_gttmin4]+1.96*_se[robots_gttmin4]
	putexcel `letter'30 = _b[robots_tmin4]+1.96*_se[robots_tmin4]
	putexcel `letter'31 = _b[robots_tmin4]+1.96*_se[robots_tmin4]
	putexcel `letter'32 = _b[robots_tmin3]+1.96*_se[robots_tmin3]
	putexcel `letter'33 = _b[robots_tmin3]+1.96*_se[robots_tmin3]
	putexcel `letter'34 = 0
	putexcel `letter'35 = 0
	putexcel `letter'36 = _b[robots_tmin1]+1.96*_se[robots_tmin1]
	putexcel `letter'37 = _b[robots_tmin1]+1.96*_se[robots_tmin1]
	putexcel `letter'38 = _b[robots_t0]-1.96*_se[robots_t0]
	putexcel `letter'39 = _b[robots_t0]+1.96*_se[robots_t0]
	putexcel `letter'40 = _b[robots_t1]-1.96*_se[robots_t1]
	putexcel `letter'41 = _b[robots_t1]+1.96*_se[robots_t1]	
	putexcel `letter'42 = _b[robots_gte2]-1.96*_se[robots_gte2]
	putexcel `letter'43 = _b[robots_gte2]+1.96*_se[robots_gte2]
	local i = `i'+2
}


////////////////////////////////////////////////////////////////////////////////
///////////////////////////6) LEADS AND LAGS////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

* Create Table B5

use "Robots and Workers\Data\BEID_PS.dta", clear
keep if manufacturing == 1

capture g sbi2 = substr(sbi,1,2)
capture destring sbi2, force replace
capture egen sectorXyear = group(sector year)

reghdfe logvalueadded robots robots_tmin1 robots_t0 robots_t1 [weight=$feweightvar], absorb($fecvar) cluster($cluster)
	estimates store weighted1
reghdfe laborshare robots robots_tmin1 robots_t0 robots_t1 [weight=$feweightvar], absorb($fecvar) cluster($cluster)
	estimates store weighted3
reghdfe loghwage robots robots_tmin1 robots_t0 robots_t1 [weight=$feweightvar], absorb($fecvar) cluster($cluster)
	estimates store weighted4
reghdfe loghoursworked robots robots_tmin1 robots_t0 robots_t1 [weight=$feweightvar], absorb($fecvar) cluster($cluster)
	estimates store weighted5

qui estimates restore weighted1
qui outreg2 using "Robots and Workers\Results\Panelregressions_leadslags", label aster replace word tex ctitle(GVA \textit{(log)}) nocon addnote(Notes: We include firm and 4-digit industry-by-year and municipality-by-year fixed effects in all specifications. We also control for linear trends of the log value added per worker in 2009, as well as linear trends of the log of workers in 2009.)
qui estimates restore weighted3
qui outreg2 using "Robots and Workers\Results\Panelregressions_leadslags", label aster append word tex ctitle(Labor share) nocon
qui estimates restore weighted4
qui outreg2 using "Robots and Workers\Results\Panelregressions_leadslags", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore weighted5
    outreg2 using "Robots and Workers\Results\Panelregressions_leadslags", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon	


////////////////////////////////////////////////////////////////////////////////
///////////////////////7) EXCLUDE WINDOW AROUND IMPLEMENTATION//////////////////
////////////////////////////////////////////////////////////////////////////////	
		
* Create Table B6

use "Robots and Workers\Data\BEID_PS.dta", clear
keep if manufacturing == 1
keep if robots_tmin1!=1 & robots_t0!=1 & robots_t1!=1 & robots_t2!=1 

reghdfe logvalueadded robots [weight=$feweightvar], absorb($fecvar) cluster($cluster),
	estimates store weighted1
reghdfe laborshare robots [weight=$feweightvar], absorb($fecvar)  cluster($cluster)
	estimates store weighted3
reghdfe loghwage robots [weight=$feweightvar], absorb($fecvar)  cluster($cluster)
	estimates store weighted4
reghdfe loghoursworked robots [weight=$feweightvar], absorb($fecvar)  cluster($cluster)
	estimates store weighted5

qui estimates restore weighted1
qui outreg2 using "Robots and Workers\Results\Panelregressions_exclwindow", label aster replace word tex ctitle(GVA \textit{(log)}) nocon addnote(Notes: We include firm and 4-digit industry-by-year and municipality-by-year fixed effects in all specifications. We also control for linear trends of the log value added per worker in 2009, as well as linear trends of the log of workers in 2009.)
qui estimates restore weighted3
qui outreg2 using "Robots and Workers\Results\Panelregressions_exclwindow", label aster append word tex ctitle(Labor share) nocon
qui estimates restore weighted4
qui outreg2 using "Robots and Workers\Results\Panelregressions_exclwindow", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore weighted5
    outreg2 using "Robots and Workers\Results\Panelregressions_exclwindow", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
	
	

	
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////8) PLACEBO TREATMENT/////////////////////////////
////////////////////////////////////////////////////////////////////////////////

* Create Table B7

use "Robots and Workers\Data\BEID_PS.dta", clear
keep if manufacturing == 1

// Create placebo-robot treatment
drop if robots==1
g rand = runiform()
gsort -robotsfirm rand
g robots_placebo = 0


replace robots_placebo = 1 if _n<=760 // Such that there are 218 unique robot-adopting firms (as in the sample)
unique beid if robots_placebo==1      // Make sure that there are indeed 218 unique robot-adopting firms

sort beid year
replace robots_placebo = 1 if robots_placebo[_n-1]==1 & beid==beid[_n-1]

reghdfe logvalueadded robots_placebo [weight=$feweightvar], absorb($fecvar) cluster($cluster)
	estimates store weighted1
reghdfe laborshare robots_placebo [weight=$feweightvar], absorb($fecvar) cluster($cluster)
	estimates store weighted3
reghdfe loghwage robots_placebo [weight=$feweightvar], absorb($fecvar)  cluster($cluster)
	estimates store weighted4
reghdfe loghoursworked robots_placebo [weight=$feweightvar], absorb($fecvar)  cluster($cluster)
	estimates store weighted5

qui estimates restore weighted1
qui outreg2 using "Robots and Workers\Results\Panelregressions_placebo", label aster replace word tex ctitle(GVA \textit{(log)}) nocon addnote(Notes: We include firm and 4-digit industry-by-year and municipality-by-year fixed effects in all specifications. We also control for linear trends of the log value added per worker in 2009, as well as linear trends of the log of workers in 2009.)
qui estimates restore weighted3
qui outreg2 using "Robots and Workers\Results\Panelregressions_placebo", label aster append word tex ctitle(Labor share) nocon
qui estimates restore weighted4
qui outreg2 using "Robots and Workers\Results\Panelregressions_placebo", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore weighted5
    outreg2 using "Robots and Workers\Results\Panelregressions_placebo", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon	
	

////////////////////////////////////////////////////////////////////////////////
/////////////////////////9) OSTER'S BIAS ADJUSTED ESTIMATES/////////////////////
////////////////////////////////////////////////////////////////////////////////

* See "Firm regressions - 03) Oster's bias-adjusted estimates.do"
	
	

////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////10) SUTVA ISSUES///////////////////////////////
////////////////////////////////////////////////////////////////////////////////	

* Create Table B9, Panel A

use "Robots and Workers\Data\BEID_PS.dta", clear
keep if manufacturing == 1

bysort sbi3: egen nrobotsfirms = max(robotsfirm)
drop if nrobotsfirms > 0 & robotsfirm==0

reghdfe logvalueadded robots [weight=$feweightvar], absorb(beid year) cluster($cluster)
	estimates store weighted1
reghdfe laborshare robots [weight=$feweightvar], absorb(beid year)  cluster($cluster)
	estimates store weighted3
reghdfe loghwage robots [weight=$feweightvar], absorb(beid year)  cluster($cluster)
	estimates store weighted4
reghdfe loghoursworked robots [weight=$feweightvar], absorb(beid year)  cluster($cluster)
	estimates store weighted5

qui estimates restore weighted1
qui outreg2 using "Robots and Workers\Results\Panelregressions_SUTVA", label aster replace word tex ctitle(GVA \textit{(log)}) nocon addnote(Notes: We include firm and 4-digit industry-by-year and municipality-by-year fixed effects in all specifications. We also control for linear trends of the log value added per worker in 2009, as well as linear trends of the log of workers in 2009.)
qui estimates restore weighted3
qui outreg2 using "Robots and Workers\Results\Panelregressions_SUTVA", label aster append word tex ctitle(Labor share) nocon
qui estimates restore weighted4
qui outreg2 using "Robots and Workers\Results\Panelregressions_SUTVA", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore weighted5
    outreg2 using "Robots and Workers\Results\Panelregressions_SUTVA", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
		

////////////////////////////////////////////////////////////////////////////////
/////////////////11) ADDRESS ISSUE OF NEGATIVE WEIGHTS IN DIDs//////////////////
////////////////////////////////////////////////////////////////////////////////	

* Create Table B9, Panel B

use "Robots and Workers\Data\BEID_PS.dta", clear
keep if manufacturing == 1

keep logvalueadded logvalueaddedphour laborshare loghwage loghoursworked capitalratio logsalesphour robots robotsfirm robots_tmin* robots_t* beid year sbi4 mun logvalueaddedpworker09 logworkers09 hoursworked09 

// Determine the year of robot adoption
sort robotsfirm beid year
g robotsyear = year*((robots==1 & robots[_n-1]==0 & robotsfirm==1) | (robots==1 & year == 2009))
bysort beid: ereplace robotsyear = max(robotsyear)

foreach t in 1 2 3 4 {
	g robotsyear_tmin`t' = (year+`t')*(robots_tmin`t'==1)
}
foreach t in 0 1 2 3 4 {
	g robotsyear_t`t' = (year-`t')*(robots_t`t'==1)
}

replace robotsyear = max(robotsyear_tmin1,robotsyear_tmin2,robotsyear_tmin3,robotsyear_tmin4,robotsyear_t0,robotsyear_t1,robotsyear_t2,robotsyear_t3,robotsyear_t4) if max(robotsyear_tmin1,robotsyear_tmin2,robotsyear_tmin3,robotsyear_tmin4,robotsyear_t0,robotsyear_t1,robotsyear_t2,robotsyear_t3,robotsyear_t4)!=0
bysort beid: ereplace robotsyear = max(robotsyear)

replace robotsyear = year-5 if robotsyear==0 & robotsfirm==1
replace robotsyear = 2009 if robotsyear<2009 & robotsfirm==1
bysort beid: ereplace robotsyear = min(robotsyear)
drop robotsyear_* robots_t*

egen beidyear = group(beid year)  // Determine unique identifier for beid-by-year to determine weights later on
save "Robots and Workers\Data\tempw_BEID_PS.dta", replace

clear all
g wave = .
forvalues year = 2009(1)2022 {
	append using "Robots and Workers\Data\tempw_BEID_PS.dta"
	replace wave = `year' if wave==.
	
	keep if robotsyear==0 | robotsyear==`year' | year!=wave
}
erase "Robots and Workers\Data\tempw_BEID_PS.dta"

duplicates tag beidyear, g(tag)
replace tag = tag+1
replace $feweightvar = $feweightvar / tag   // Adjust weight variable according to number of the same observation in the data

global fecvaradj beid#wave sbi4#year#wave mun#year#wave c.logvalueaddedpworker09#year#wave c.logworkers09#year#wave

reghdfe logvalueadded robots [weight=$feweightvar], absorb($fecvaradj) cluster($cluster)
	estimates store weighted1
reghdfe laborshare robots [weight=$feweightvar], absorb($fecvaradj)  cluster($cluster)
	estimates store weighted3
reghdfe loghwage robots [weight=$feweightvar], absorb($fecvaradj)  cluster($cluster)
	estimates store weighted4
reghdfe loghoursworked robots [weight=$feweightvar], absorb($fecvaradj)  cluster($cluster)
	estimates store weighted5

	
qui estimates restore weighted1
qui outreg2 using "Robots and Workers\Results\Panelregressions_negweights", label aster replace word tex ctitle(GVA \textit{(log)}) nocon addnote(Notes: We include firm and 4-digit industry-by-year and municipality-by-year fixed effects in all specifications. We also control for linear trends of the log value added per worker in 2009, as well as linear trends of the log of workers in 2009.)
qui estimates restore weighted3
qui outreg2 using "Robots and Workers\Results\Panelregressions_negweights", label aster append word tex ctitle(Labor share) nocon
qui estimates restore weighted4
qui outreg2 using "Robots and Workers\Results\Panelregressions_negweights", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore weighted5
    outreg2 using "Robots and Workers\Results\Panelregressions_negweights", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon
	

////////////////////////////////////////////////////////////////////////////////
///////////////////////////12) UNWEIGHTED///////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

* Create Table B10, columns (1)-(4)

use "Robots and Workers\Data\BEID_PS.dta", clear
keep if manufacturing == 1

// Regressions
reghdfe logvalueadded robots, absorb($fecvar) cluster($cluster)
estimates store unweighted1
reghdfe laborshare robots, absorb($fecvar)  cluster($cluster)
estimates store unweighted3
reghdfe loghwage robots, absorb($fecvar)  cluster($cluster)
estimates store unweighted4
reghdfe loghoursworked robots, absorb($fecvar)  cluster($cluster)
estimates store unweighted5

qui estimates restore unweighted1
qui outreg2 using "Robots and Workers\Results\Panelregressions_unweightedallfirms", label aster replace word tex ctitle(GVA \textit{(log)}) nocon addnote(Notes: We include firm and 4-digit industry-by-year and municipality-by-year fixed effects in all specifications. We also control for linear trends of the log value added per worker in 2009, as well as linear trends of the log of workers in 2009.)
qui estimates restore unweighted3
qui outreg2 using "Robots and Workers\Results\Panelregressions_unweightedallfirms", label aster append word tex ctitle(Labor share) nocon
qui estimates restore unweighted4
qui outreg2 using "Robots and Workers\Results\Panelregressions_unweightedallfirms", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore unweighted5
qui outreg2 using "Robots and Workers\Results\Panelregressions_unweightedallfirms", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon	


////////////////////////////////////////////////////////////////////////////////
///////////////////////////13) USE ALL FIRMS////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

* Create Table B10, columns (5)-(8)

preserve
use "Robots and Workers\Data\BEID_PS.dta", clear
	reghdfe logvalueadded robots [weight=$feweightvar], absorb($fecvar) cluster($cluster)
	estimates store weighted1
	reghdfe laborshare robots [weight=$feweightvar], absorb($fecvar) cluster($cluster)
	estimates store weighted3
	reghdfe loghwage robots [weight=$feweightvar], absorb($fecvar) cluster($cluster)
	estimates store weighted4
	reghdfe loghoursworked robots [weight=$feweightvar], absorb($fecvar) cluster($cluster)
	estimates store weighted5
restore

qui estimates restore weighted1
qui outreg2 using "Robots and Workers\Results\Panelregressions_unweightedallfirms", label aster append word tex ctitle(GVA \textit{(log)}) nocon
qui estimates restore weighted3
qui outreg2 using "Robots and Workers\Results\Panelregressions_unweightedallfirms", label aster append word tex ctitle(Labor share) nocon
qui estimates restore weighted4
qui outreg2 using "Robots and Workers\Results\Panelregressions_unweightedallfirms", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
qui estimates restore weighted5
    outreg2 using "Robots and Workers\Results\Panelregressions_unweightedallfirms", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon	
	

////////////////////////////////////////////////////////////////////////////////
///////////////////////14) LONGER PANEL FROM 2004 ONWARDS///////////////////////
////////////////////////////////////////////////////////////////////////////////

* Create Table B10, columns (9)-(12)

preserve
	use "Robots and Workers\Data\BEID_PS_20002019.dta", clear
	keep if manufacturing == 1
	global extminyear = 2004
	keep if year >= $extminyear
	drop if robots_mt==1
	duplicates tag beid, g(tag)
	drop if tag < (2019- $extminyear)
	drop tag
	unique beid
	unique beid if robots == 1

	tsset beid year
	sort beid year
	replace robots = 0 if year < 2009
	bysort beid: ereplace robotsfirm = max(robotsfirm) 
	g robotsyear = year if robots==1
	bysort beid: ereplace robotsyear = min(robotsyear)
	keep if robotsyear>=2014 | robotsyear==.

	tostring sbi4, force replace
	capture g sbi2 = substr(sbi4,1,2)
	capture g sbi3 = substr(sbi4,1,3)
	capture destring sbi2, force replace
	capture destring sbi3, force replace
	capture destring sbi4, force replace

	bysort beid: ereplace corop = mode(corop), minmode    // Keep dominant municipality
	bysort beid: ereplace mun = mode(mun), minmode    // Keep dominant municipality
	bysort beid: ereplace sbi2 = mode(sbi2), minmode      // Keep dominant 2-digit sector
	bysort beid: ereplace sbi3 = mode(sbi3), minmode      // Keep dominant 3-digit sector
	bysort beid: ereplace sbi4 = mode(sbi4), minmode      // Keep dominant 4-digit sector
	bysort sbi2: egen count = count(beid)
	drop if count < 20

	g logvalueaddedpworker$extminyear = logvalueaddedpworker if year == $extminyear
	bysort beid: ereplace logvalueaddedpworker$extminyear = mean(logvalueaddedpworker$extminyear)
	g logworkers$extminyear = logworkers if year == $extminyear
	bysort beid: ereplace logworkers$extminyear = mean(logworkers$extminyear)

	// Regressions
	reghdfe logvalueadded robots [weight=$feweightvar], absorb(beid mun#year sbi4#year logvalueaddedpworker$extminyear logworkers$extminyear) cluster($cluster)
		estimates store weighted1
	reghdfe laborshare robots [weight=$feweightvar], absorb(beid mun#year sbi4#year logvalueaddedpworker$extminyear logworkers$extminyear)  cluster($cluster)
		estimates store weighted3
	reghdfe loghwage robots [weight=$feweightvar], absorb(beid mun#year sbi4#year logvalueaddedpworker$extminyear logworkers$extminyear)  cluster($cluster)
		estimates store weighted4
	reghdfe loghoursworked robots [weight=$feweightvar], absorb(beid mun#year sbi4#year logvalueaddedpworker$extminyear logworkers$extminyear)  cluster($cluster)
		estimates store weighted5


	qui estimates restore weighted1
	qui outreg2 using "Robots and Workers\Results\Panelregressions_unweightedallfirms", label aster append word tex ctitle(GVA \textit{(log)}) nocon
	qui estimates restore weighted3
	qui outreg2 using "Robots and Workers\Results\Panelregressions_unweightedallfirms", label aster append word tex ctitle(Labor share) nocon
	qui estimates restore weighted4
	qui outreg2 using "Robots and Workers\Results\Panelregressions_unweightedallfirms", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon
	qui estimates restore weighted5
		outreg2 using "Robots and Workers\Results\Panelregressions_unweightedallfirms", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon	
restore	


////////////////////////////////////////////////////////////////////////////////
////////////////////15) WITHIN-FIRM WAGE INEQUALITYWAGE VARIANCE ///////////////
////////////////////////////////////////////////////////////////////////////////		

* Create Table B11

// Obtain wage variance within the firm	
use "Robots and Workers\Data\Workers_PS_LFS_panel_full.dta", clear
 	sort rinpersoon year
 	keep if include==1 
	
	g empl = 1-nonempl
	replace empl = . if (jobtype_selfempl==1 | jobtype_retired==1 | jobtype_student==1 | jobtype_other==1 | jobtype_employee ==.)
	capture g sbi2 = substr(sbi,1,2)
	capture destring sbi2, force replace	
	keep if empl==1
	collapse (sd) hwage (sum) empl, by(beid year)
	rename (hwage empl) (sd_hwage sd_count)
	g log_varhwage = ln(sd_hwage^2)
	save "Robots and Workers\Data\BEID_wagesd.dta", replace
	
// Match wage variance to the firm	
use "Robots and Workers\Data\BEID_PS.dta", clear
	keep if manufacturing == 1
	merge 1:1 beid year using "Robots and Workers\Data\BEID_wagesd.dta", nogen keep(1 3)
	erase "Robots and Workers\Data\BEID_wagesd.dta"
	
reghdfe log_varwage robots [weight=$feweightvar], absorb($fecvar) cluster($cluster), if sd_count>1
qui estimates store firms_wagevar1
reghdfe log_varwage robots [weight=$feweightvar], absorb($fecvar) cluster($cluster), if sd_count>=5
qui estimates store firms_wagevar2
reghdfe log_varwage robots [weight=$feweightvar], absorb($fecvar) cluster($cluster), if sd_count>=10	
qui estimates store firms_wagevar3

qui estimates restore firms_wagevar1
qui outreg2 using "Robots and Workers\Results\Panelregressions_wagevariance", label aster replace word tex ctitle(Robot adopter) nocon addnote(Notes: The dependent variable equals one when a firm will adopt robots in the future. The regression is estimated for the year 2009. We include firm and 4-digit industry and municipality fixed effects in all specifications. We do not weight the results.)
qui estimates restore firms_wagevar2
qui outreg2 using "Robots and Workers\Results\Panelregressions_wagevariance", label aster append word tex ctitle(Robot adopter) nocon
qui estimates restore firms_wagevar3
    outreg2 using "Robots and Workers\Results\Panelregressions_wagevariance", label aster append word tex ctitle(Robot adopter) nocon



